package net.hangar5.xmlrpc;

/* RpcClient.java

The contents of this file are subject to the Mozilla Public
License Version 1.1 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.

The Original Code is "Hangar5 XMLRPC Library".

The Initial Developer of the Original Code is James D. Rudnicki.
Portions created by James D. Rudnicki are
Copyright (C) 2001.  All Rights Reserved.

Contributor(s):
*/
import java.net.*;
import java.util.*;
import java.io.*;

/** Client agent for XML-RPC calls.
 *
 * @author  James Rudnicki
 * @version
 */
public class RpcClient {

  protected HttpClient myClient;

   public RpcClient()
   {
   	myClient = new HttpClient();
	myClient.setContentType( HttpClient.byXml );
   }

  /** Creates new RpcClient */
  public RpcClient( String strServer ) throws IOException, MalformedURLException {
	URL urlServer = new URL( strServer );
	myClient = new HttpClient( urlServer );
	myClient.setContentType( HttpClient.byXml );
	return;
  }
  /** Creates new RpcClient */
  public RpcClient( URL urlServer ) throws IOException {
	myClient = new HttpClient( urlServer );
	return;
  }

  public void setUrl(String strServer) throws IOException, MalformedURLException
  {
  	URL urlServer = new URL( strServer );
	myClient.setUrl( urlServer );
  }



  /** Call method at specified URI.
   * null for strUri indicates default URI.
   */
  public Object execute( String strUri, String strMethod, Vector vParams ) throws RpcException {
	Object oRet = null;
	StringBuffer sbCall;
	sbCall = CallWriter.write( strMethod, vParams );
	try {
	  if( null==strUri ) {
		myClient.writePost( sbCall.toString().getBytes() );
	  }
	  else {
		myClient.writePost( strUri, sbCall.toString().getBytes() );
	  }
	  HttpResponse hr = myClient.getResponse();
	  String strResp = hr.getContent();
	  if( (null!=strResp) && (strResp.length() > 0 ) ) {
		RespParser rp = new RespParser( strResp );
		oRet = rp.parseXml();
	  }
	  else {
		throw new RpcException( RpcException.GENERIC, "No response" );
	  }
	}
	catch( RpcException x ) {
	  oRet = null;
	  throw x;
	}
	catch( IOException x ) {
	  oRet = null;
	}
	return oRet;
  }
  /** Call method using current URI.
   */
  public Object execute( String strMethod, Vector vParams ) throws RpcException {
	return execute( null, strMethod, vParams );
  }
  /** Shutdown a keep-alive connection.  Client can call this
   * when a group of calls have been completed.
   */
  public void finished() {
	myClient.closeConnection();
	return;
  }
  public boolean getKeepAlive() {
	return myClient.getKeepAlive();
  }
  public String getUri() {
	return myClient.getUri();
  }
  public void setKeepAlive( boolean bNew ) {
	myClient.setKeepAlive(bNew);
  }
  public void setUri( String strNew ) {
	myClient.setUri(strNew);
  }
} // end class
